from datetime import datetime, timedelta


class Monitor:
    def __init__(self, config_data):
        self.config_data = config_data
        self.car_min_speed = float(config_data['car_min_speed'])
        self.event = {}
        self.event_time = []
        self.content = ""
        self.level = 0

    def flow(self, data, last_data):
        # 流量变化较大
        flow = self.config_data["flow"]
        up_flow = data["up_flow_num"] - last_data["up_flow_num"]
        down_flow = data["down_flow_num"] - last_data["down_flow_num"]
        if up_flow > 10:
            if up_flow > flow * last_data["up_flow_num"]:
                return True
        if down_flow > 10:
            if down_flow > flow * last_data["down_flow_num"]:
                return True
        return False

    def flow_match(self, data, last_data):
        # 匹配流量变化较大
        flow = self.config_data["flow"]
        match = data["total_match"] - last_data["total_match"]
        if match > 10:
            if match > flow * last_data["total_match"]:
                return True
            if match > 20:
                return True
        return False

    def car_speed(self, data, last_data, vtype="1"):
        # 小客车速度骤降
        speed = self.config_data["speed"]
        if vtype not in data["type_speed_dict"].keys() or vtype not in last_data["type_speed_dict"].keys():
            return False
        current_speed = data["type_speed_dict"][vtype]["mean_speed"]
        last_speed = last_data["type_speed_dict"][vtype]["mean_speed"]
        speed_diff = last_speed - current_speed
        if speed_diff > speed * last_speed:
            return True
        if current_speed < self.car_min_speed:
            return True
        return False

    def car_slow_num(self, data, vtype="1"):
        # 小客车慢行数量过多
        if vtype not in data["type_speed_dict"].keys():
            return 0
        current_num = data["type_speed_dict"][vtype]["slow_level_count"]
        level = current_num // 10
        return level

    def flow_empty(self, data):
        up_flow = data["up_flow_num"]
        down_flow = data["down_flow_num"]
        if up_flow == 0 and down_flow == 0:
            return True
        return False

    def pass_rate(self, data, vtype="1"):
        pass_rate = self.config_data["pass_rate"]
        if vtype not in data["type_pass_rate_dict"].keys():
            return False
        car_pass_rate = data["type_pass_rate_dict"][vtype]
        current_pass_rate = int(car_pass_rate["vaild_count"] / car_pass_rate["count"] * 100)
        if current_pass_rate < pass_rate:
            return True
        return False

    def run(self, data, last_data):
        if last_data is not None:
            self.content = ""
            self.level = 0
            event_time = data["time"]
            flow_flag = self.flow(data, last_data)
            speed_flag = self.car_speed(data, last_data)
            slow_level = self.car_slow_num(data)
            flow_empty_flag = self.flow_empty(data)
            pass_flag = self.pass_rate(data)
            if flow_empty_flag:
                self.content += "车流量为零 "
                self.level = 4
            elif slow_level == 0:
                if flow_flag:
                    self.content += "车流量骤降 "
                    self.level = 1
                if speed_flag:
                    self.content += "平均车速过低 "
                    self.level = 1
                if pass_flag:
                    self.content += "通行率过低 "
                    self.level = 1
            elif slow_level == 1:
                self.content += "慢行车辆较多 "
                self.level = 1
                if flow_flag:
                    self.content += "车流量骤降 "
                    self.level = 2
                if speed_flag:
                    self.content += "平均车速过低 "
                    self.level = 2
                if pass_flag:
                    self.content += "通行率过低 "
                    self.level = 2
            elif slow_level == 2:
                self.content += "慢行车辆多 "
                self.level = 2
                if flow_flag:
                    self.content += "车流量骤降 "
                    self.level = 3
                if speed_flag:
                    self.content += "平均车速过低 "
                    self.level = 3
                if pass_flag:
                    self.content += "通行率过低 "
                    self.level = 3
            elif slow_level == 3:
                self.content += "慢行车辆很多 "
                self.level = 3
                if flow_flag:
                    self.content += "车流量骤降 "
                    self.level = 4
                if speed_flag:
                    self.content += "平均车速过低 "
                    self.level = 4
                if pass_flag:
                    self.content += "通行率过低 "
                    self.level = 4
            else:
                self.content += "慢行车辆太多 "
                self.level = 4

            if self.level == 0:
                return None
            else:
                c = f"time: {event_time}, level: {self.level}, content: {self.content}"
                return c
            # if flow_empty_flag:
            #     content = "车流量为零" + event_time.strftime(self.config_data["format_str"])
            #     return content
            # if flow_flag and speed_flag:
            #     if pass_flag:
            #         content = "车流量过大，车速过低，车速过慢，通过率过低" + event_time.strftime(self.config_data["format_str"])
            #         return content
            #     else:
            #         content = "车流量过大，车速过低，车速过慢" + event_time.strftime(self.config_data["format_str"])
            #         return content
            # if speed_flag or slow_flag:
            #     content = "车速过低，慢行车辆过多" + event_time.strftime(self.config_data["format_str"])
            #     return content
        return None


if __name__ == '__main__':
    print("2222222222222")
